package 练习06_线程池模拟批量导入数据;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: TestThreadpoolImport01
 * @Author: AllenSun
 * @Date: 2022/3/22 下午10:29
 */
public class TestThreadpoolImport01 {

    private static ThreadPoolExecutor executor = new ThreadPoolExecutor(0,25,100L,
            TimeUnit.SECONDS,new LinkedBlockingQueue<>(),new ThreadPoolExecutor.CallerRunsPolicy());
    public static void main(String[] args) {
        Student student = null;
        List<Student> studentList = new ArrayList<>();
        int age = 0;
        int heigh = 0;
        //添加50万个数据
        for (int i=0;i<=500000;i++) {
            age = (int) Math.floor((Math.random() * 10) + 20);
            heigh = (int) Math.floor((Math.random() * 160) + 180);
            student = new Student("name"+i,age,heigh,"玩");
            studentList.add(student);
        }

        long start = System.currentTimeMillis();
        for (Student student1:studentList) {
            //模拟对数据信息进行二次处理
            executor.submit(()->student1.setName(student1.getName()+"这是后缀"));
        }
        long end = System.currentTimeMillis();
        System.out.println("添加数量："+studentList.stream().filter(x->x.getName().contains("这是后缀")).count());
        System.out.println("花费时间："+(end-start));


        //输出的数据是
        // 添加数量：371014
        // 花费时间：114
        //不是50w，这是由于线程池中的子线程任务没有执行完，而主线程已经开始执行业务代码，导致成功数量变少。

    }
}
